$(function () {

    $("body").on('click','#bindMobileBtn',function () {//点击绑定或修改绑定手机号
        var oldMobile = $("input[name='mobile']").val();
        if(oldMobile==null || $.trim(oldMobile)==''){//没绑定过手机号
            openMobile2(null);
        }else{//绑定过手机号
            $("#mobile1").removeAttr("disabled") .attr("value",oldMobile).attr("disabled","true");
            openMobile1(oldMobile);
        }
    }).on('click','#bindMailBtn',function () {
        var oldEmail = $("input[name='email']").val();
        if(oldEmail==null || $.trim(oldEmail)==''){//没绑定过邮箱号
            openEmail2(null);
        }else{//绑定过邮箱号
            $("#email1").removeAttr("disabled") .attr("value",oldEmail).attr("disabled","true");
            openEmail1(oldEmail);
        }
    });
    //======================================绑定手机号，开始====================================
    /**
     * 第一步，用于绑定过手机号的
     */
    function openMobile1(oldMobile) {
        var _ele = $("#achangeMle1").html();
        icModale(2, '<strong>验证原手机号</strong>', _ele, '下一步', '520px', function(index, ele) {
            var oldMobile = ele.find('#mobile1').val();
            var oldMobileCode = ele.find('#code1').val();
            //验证原的手机号与验证码
            $.ajax({
                url:baseloaction +'/web/uc/binding/verificationOldMobile',
                type:'post',
                dataType:'json',
                data:{
                    'mobile':oldMobile,
                    'mobileCode':oldMobileCode
                },
                success:function (result) {
                    if(result.success==true){
                        //关闭窗口
                        layer.close(index);
                        //验证成功，路径第二步
                        openMobile2(oldMobileCode);
                    }else{
                        icModale(0,'',result.message);
                    }
                },
                error:function (error) {
                    icModale(0,'','系统错误，原手机号验证失败');
                }
            });
        });
        //发送短信
        $("body").on('click','#sendUnBindingCode',function () {
            var _this = $(this);
            _this.hide();
            _this.next().show();
            _this.next().text("(60s)后重新获取");
            var mobile = $("#mobile1").val();
            sendBindingMobile(mobile,4,function () {
                var time = 60;
                var it = setInterval(function () {
                    time --;
                    if(time <=0 ){
                        window.clearInterval(it);
                        _this.show();
                        _this.next().hide();
                        _this.next().text("(60s)后重新获取");
                    }else{
                        _this.next().text("("+time+"s)后重新获取");
                    }
                },1000);
            });
        });
    }

    /**
     * 第二步，用于没有绑定过手机或更换新绑定手机号的
     */
    function openMobile2(oldMobileCode) {
        var _ele = $("#achangeMle2").html();
        icModale(2, '<strong>绑定新手机号</strong>', _ele, '确认', '520px', function(index, ele) {
            var mobile = ele.find('#mobile2').val();
            var mobileCode = ele.find('#code2').val();
            if(oldMobileCode==null){
                oldMobileCode = '';
            }
            //执行绑定
            bindingMobile(mobile,mobileCode,oldMobileCode,index);
        });
        $("body").on('click','#sendBindingCode',function () {
            var _this = $(this);
            _this.hide();
            _this.next().show();
            _this.next().text("(60s)后重新获取");
            var mobile =  $('div.layui-layer').find("#mobile2").val();
            sendBindingMobile(mobile,3,function () {
                var time = 60;
                var it = setInterval(function () {
                    time --;
                    if(time <=0 ){
                        window.clearInterval(it);
                        _this.show();
                        _this.next().hide();
                        _this.next().text("(60s)后重新获取");
                    }else{
                        _this.next().text("("+time+"s)后重新获取");
                    }
                },1000);
            });
        });
    }

    /**
     * 绑定手机号
     * @param mobile 新的手机号
     * @param mobileCode 新手机号验证码
     * @param oldMobileCode 原手机号验证码（如果有就传，没有就传null）
     * @param index 弹框的下标
     */
    function bindingMobile(mobile,mobileCode,oldMobileCode,index) {
        $.ajax({
            url:baseloaction +'/web/uc/binding/bindingUserMobile',
            type:'post',
            dataType:'json',
            data:{
                'mobile':mobile,
                'mobileCode':mobileCode,
                'oldMobileCode':oldMobileCode
            },
            success:function (result) {
                if(result.success==true){
                    //关闭窗口
                    layer.close(index);
                    $("#bindMobileBtn").find("span").text('修改绑定');
                    $("#bindMobileEd").find("span").text('已绑定');
                    $(".mobileNewBtn").find("span").text(mobile);
                    $(".mobileNewBtn").find("span").removeAttr("class");
                    $(".mobileNewIcon").addClass("bind");
                    $("input[name='mobile']").val(mobile);
                }else{
                    icModale(0,'',result.message);
                }
            },
            error:function (error) {
                icModale(0,'','系统错误，手机绑定失败');
            }
        });
    }

    /**
     * 发送短信验证码
     * @param mobile 手机号
     * @param status 状态，3绑定 ，4更换
     */
    function sendBindingMobile(mobile,status,callback) {
        $.ajax({
            url:baseloaction +'/web/uc/binding/sendBindingMobileCode',
            type:'post',
            data:{
                'mobile':mobile,
                'status':status
            },
            dataType:'json',
            success:function (result) {
                if(result.success==true){
                    callback();
                }else{
                    icModale(0,'',result.message);
                }
            },
            error:function (error) {
                icModale(0,'','系统错误，验证短信发送失败');
            }
        });
    }
    //======================================绑定手机号，结束====================================

    //======================================绑定邮箱号，开始====================================
    /**
     * 第一步，用于绑定过邮箱号的
     */
    function openEmail1(oldEmail) {
        var _ele = $("#achangeEle1").html();
        icModale(2, '<strong>验证原邮箱号</strong>', _ele, '下一步', '520px', function(index, ele) {
            var oldEmail = ele.find('#email1').val();
            var oldEmailCode = ele.find('#codeEmail1').val();
            //验证原的手机号与验证码
            $.ajax({
                url:baseloaction +'/web/uc/binding/verificationOldEmail',
                type:'post',
                dataType:'json',
                data:{
                    'email':oldEmail,
                    'emailCode':oldEmailCode
                },
                success:function (result) {
                    if(result.success==true){
                        //关闭窗口
                        layer.close(index);
                        //验证成功，路径第二步
                        openEmail2(oldEmailCode);
                    }else{
                        icModale(0,'',result.message);
                    }
                },
                error:function (error) {
                    icModale(0,'','系统错误，原邮箱号验证失败');
                }
            });
        });
        //发送邮箱验证
        $("body").on('click','#sendUnBindingEmailCode',function () {
            var _this = $(this);
            var email = $("#email1").val();
            sendBindingEmail(email,4,function () {
                _this.hide();
                _this.next().show();
                _this.next().text("(60s)后重新获取");
                var time = 60;
                var it = setInterval(function () {
                    time --;
                    if(time <=0 ){
                        window.clearInterval(it);
                        _this.show();
                        _this.next().hide();
                        _this.next().text("(60s)后重新获取");
                    }else{
                        _this.next().text("("+time+"s)后重新获取");
                    }
                },1000);
            });
        });
    }

    /**
     * 第二步，用于没有绑定过手机或更换新绑定邮箱号的
     */
    function openEmail2(oldEmailCode) {
        var _ele = $("#achangeEle2").html();
        icModale(2, '<strong>绑定邮箱号</strong>', _ele, '确认', '520px', function(index, ele) {
            var email = ele.find('#email2').val();
            var emailCode = ele.find('#codeEmail2').val();
            if(oldEmailCode==null){
                oldEmailCode = '';
            }
            //执行绑定
            bindingEmail(email,emailCode,oldEmailCode,index);
        });
        $("body").on('click','#sendBindingEmailCode',function () {
            var _this = $(this);
            var email =  $('div.layui-layer').find("#email2").val();
            sendBindingEmail(email,3,function () {
                _this.hide();
                _this.next().show();
                _this.next().text("(60s)后重新获取");
                var time = 60;
                var it = setInterval(function () {
                    time --;
                    if(time <=0 ){
                        window.clearInterval(it);
                        _this.show();
                        _this.next().hide();
                        _this.next().text("(60s)后重新获取");
                    }else{
                        _this.next().text("("+time+"s)后重新获取");
                    }
                },1000);
            });
        });
    }
    /**
     * 绑定邮箱号
     * @param email 新的邮箱号
     * @param emailCode 新邮箱号验证码
     * @param oldEmailCode 原邮箱号验证码（如果有就传，没有就传null）
     * @param index 弹框的下标
     */
    function bindingEmail(email,emailCode,oldEmailCode,index) {
        $.ajax({
            url:baseloaction +'/web/uc/binding/bindingUserEmail',
            type:'post',
            dataType:'json',
            data:{
                'email':email,
                'emailCode':emailCode,
                'oldEmailCode':oldEmailCode
            },
            success:function (result) {
                if(result.success==true){
                    //关闭窗口
                    layer.close(index);
                    $("#bindMailBtn").find("span").text('修改绑定');
                    $("#bindMailEd").find("span").text('已绑定');
                    $(".emailNewBtn").find("span").text(email);
                    $(".emailNewBtn").find("span").removeAttr("class");
                    $(".emailNewIcon").addClass("bind");
                    $("input[name='email']").val(email);
                }else{
                    icModale(0,'',result.message);
                }
            },
            error:function (error) {
                icModale(0,'','系统错误，邮箱绑定失败');
            }
        });
    }
    /**
     * 发送邮箱验证码
     * @param email 邮箱号
     * @param status 状态，3绑定 ，4更换
     */
    function sendBindingEmail(email,status,callback) {
        $.ajax({
            url:baseloaction +'/web/uc/binding/sendBindingEmailCode',
            type:'post',
            data:{
                'email':email,
                'status':status
            },
            dataType:'json',
            success:function (result) {
                if(result.success==true){
                    callback();
                }else{
                    icModale(0,'',result.message);
                }
            },
            error:function (error) {
                icModale(0,'','系统错误，验证邮件发送失败');
            }
        });
    }
    //======================================绑定邮箱号，结束====================================
});